package com.lihaogn.sell.web;

import com.lihaogn.sell.entity.User;
import com.lihaogn.sell.service.UserService;
import com.lihaogn.sell.vo.ListVo;
import com.lihaogn.sell.vo.SearchPageVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @FileName: UserController
 * @Description: TODO
 * @Author: lihao
 * @Date: 2019-05-17 11:50
 * @Version: 1.0
 */
@Controller
@Api(tags = "用户数据接口")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private ListVo listVo;

    // ======================= 跳转页面 =======================

    @ApiIgnore
    @RequestMapping(value = "/userShow")
    private String userShow() {
        return "user/user-list";
    }

    // ======================= 操作 =======================

    // ========== 查询 ==========

    @GetMapping(value = "/userSearch")
    @ResponseBody
    private ListVo userSearch(SearchPageVo searchPageVo) {

        return userService.searchUser(searchPageVo);

    }

    @GetMapping(value = "/showAllUser")
    @ResponseBody
    private ListVo showAllUser(String page,String limit) {
        List<User> userList = userService.getAllUserForPage(page,limit);

        listVo.setCode(0);
        listVo.setMsg("success");
        listVo.setCount(userService.getUserRowsCount());
        listVo.setData(userList);

        return listVo;

    }

    // ========== 添加 ==========

    @PostMapping(value = "/addOneUser")
    @ResponseBody
    private Integer addOneUser(User user) {
        int i = userService.addOneUser(user);

        return i;
    }

    // ========== 修改 ==========

    @PutMapping(value = "/updateOneUser")
    @ResponseBody
    private Integer updateOneUser(User user) {
        int i = userService.updateOneUser(user);
        return i;
    }

    // ========== 删除 ==========

    @DeleteMapping(value = "/deleteOneUser")
    @ResponseBody
    private Integer deleteOneUser(String userId) {
        int i = userService.deleteOneUser(userId);
        return i;
    }

    @DeleteMapping(value = "/deleteMultiUser")
    @ResponseBody
    private Integer deleteMultiUser(HttpServletRequest request) {
        return userService.deleteMultiUser(request);
    }

}
